import java.util.ArrayList;
import java.util.List;import java.util.Objects;
// 客户类class Customer {
    private final String customerId; // 不可变ID
    private String name;
    private final List<Order> orders = new ArrayList<>(); // 初始化不可变集合引用

    public Customer(String customerId, String name) {
        // 校验ID非空
        this.customerId = Objects.requireNonNull(customerId, "客户ID不能为空");
        this.name = Objects.requireNonNull(name, "客户姓名不能为空");
    }

    // 添加订单并维护双向关联
    public void addOrder(Order order) {
        if (order != null && !orders.contains(order)) {
            orders.add(order);
            // 防止循环调用，仅在订单未关联当前客户时设置
            if (order.getCustomer() != this) {
                order.setCustomer(this);
            }
        }
    }

    // 移除订单（补充必要操作）
    public void removeOrder(Order order) {
        if (order != null) {
            orders.remove(order);
            if (order.getCustomer() == this) {
                order.setCustomer(null);
            }
        }
    }

    // Getter和Setter（仅暴露必要修改接口）
    public String getCustomerId() {
        return customerId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = Objects.requireNonNull(name, "客户姓名不能为空");
    }

    // 返回订单不可修改视图，防止外部篡改
    public List<Order> getOrders() {
        return List.copyOf(orders);
    }}
// 订单类class Order {
    private final String orderId;
    private Customer customer;
    private final List<OrderDetail> orderDetails = new ArrayList<>();
    private Payment payment;

    public Order(String orderId) {
        this.orderId = Objects.requireNonNull(orderId, "订单ID不能为空");
    }
    // 关联客户（封装客户设置逻辑）
    public void setCustomer(Customer customer) {
        // 解除与原客户的关联
        if (this.customer != null && this.customer != customer) {
            this.customer.removeOrder(this);
        }
        this.customer = customer;
        // 建立与新客户的关联
        if (customer != null && !customer.getOrders().contains(this)) {
            customer.addOrder(this);
        }
    }
    // 添加订单详情（校验商品和数量合法性）
    public void addOrderDetail(OrderDetail orderDetail) {
        if (orderDetail == null) {
            throw new IllegalArgumentException("订单详情不能为空");
        }
        if (orderDetail.getQuantity() <= 0) {
            throw new IllegalArgumentException("商品数量必须大于0");
        }
        if (!orderDetails.contains(orderDetail)) {
            orderDetails.add(orderDetail);
        }
    }
    // 设置支付方式（确保一个订单只有一种支付）
    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    // Getter
    public String getOrderId() {
        return orderId;
    }

    public Customer getCustomer() {
        return customer;
    }

    public List<OrderDetail> getOrderDetails() {
        return List.copyOf(orderDetails);
    }

    public Payment getPayment() {
        return payment;
    }}
// 订单详情类class OrderDetail {
    private final Product product; // 商品不可变
    private int quantity;

    public OrderDetail(Product product, int quantity) {
        this.product = Objects.requireNonNull(product, "商品不能为空");
        if (quantity <= 0) {
            throw new IllegalArgumentException("商品数量必须大于0");
        }
        this.quantity = quantity;
    }

    // 允许修改数量（但需校验）
    public void setQuantity(int quantity) {
        if (quantity <= 0) {
            throw new IllegalArgumentException("商品数量必须大于0");
        }
        this.quantity = quantity;
    }

    // Getter
    public Product getProduct() {
        return product;
    }

    public int getQuantity() {
        return quantity;
    }}
// 商品类class Product {
    private final String productId;
    private String name;

    public Product(String productId, String name) {
        this.productId = Objects.requireNonNull(productId, "商品ID不能为空");
        this.name = Objects.requireNonNull(name, "商品名称不能为空");
    }

    // Getter和Setter
    public String getProductId() {
        return productId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = Objects.requireNonNull(name, "商品名称不能为空");
    }}
// 支付基类（抽象化，避免直接实例化）abstract class Payment {
    private final String paymentId;

    public Payment(String paymentId) {
        this.paymentId = Objects.requireNonNull(paymentId, "支付ID不能为空");
    }

    public String getPaymentId() {
        return paymentId;
    }}
// 信用卡支付class Credit extends Payment {
    private final String cardNumber; // 卡号不可变

    public Credit(String paymentId, String cardNumber) {
        super(paymentId);
        this.cardNumber = Objects.requireNonNull(cardNumber, "卡号不能为空");
    }

    public String getCardNumber() {
        return cardNumber;
    }}
// 现金支付class Cash extends Payment {
    private double amount;

    public Cash(String paymentId, double amount) {
        super(paymentId);
        if (amount <= 0) {
            throw new IllegalArgumentException("现金金额必须大于0");
        }
        this.amount = amount;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("现金金额必须大于0");
        }
        this.amount = amount;
    }}
// 在线转账class WireTransfer extends Payment {
    private final String bankAccount;

    public WireTransfer(String paymentId, String bankAccount) {
        super(paymentId);
        this.bankAccount = Objects.requireNonNull(bankAccount, "银行账号不能为空");
    }

    public String getBankAccount() {
        return bankAccount;
    }}
// 支付宝class AliPay extends Payment {
    private final String account;

    public AliPay(String paymentId, String account) {
        super(paymentId);
        this.account = Objects.requireNonNull(account, "支付宝账号不能为空");
    }

    public String getAccount() {
        return account;
    }}
// 微信支付class WeixinPay extends Payment {
    private final String account;

    public WeixinPay(String paymentId, String account) {
        super(paymentId);
        this.account = Objects.requireNonNull(account, "微信账号不能为空");
    }

    public String getAccount() {
        return account;
    }}